package com.bjesc_b2c.app.appraiset.base;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.view.Window;
import android.widget.Toast;

import com.bjesc_b2c.app.appraiset.MyApplication;
import com.bjesc_b2c.app.appraiset.widget.dialog.LoadingDialog;
import com.example.codeutils.utils.KeyboardUtils;

import butterknife.ButterKnife;
import rxbus_base.SuperRxBusBaseActivity;

/**
 * Created by Jason Chen on 2017/2/20.
 */

public abstract class BaseActivity extends SuperRxBusBaseActivity {
    protected Toast toast;
    protected Activity mContext;
    private LoadingDialog mLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        mContext = this;
        MyApplication.getInstance().addActivity(this);
        initEventAndData();
        createPresenter();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        cancelLoading();
    }
    @Override
    protected void onRestart() {
        super.onRestart();
    }
    protected void showToast(String text){
        if(toast == null){
            toast = Toast.makeText(this,text,Toast.LENGTH_LONG);
        }else{
            toast.setText(text);
        }
        toast.show();
    }
    protected String getResourceString(int resourceId){
        return getResources().getString(resourceId);
    }
    protected void startIntent(Intent intent ,boolean isFinish){
        startActivity(intent);
        if(isFinish){
            finish();
        }
    }
    protected void startIntent(Intent intent,int requestId){
        startActivityForResult( intent, requestId);
    }
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics() );
        return res;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLoading != null) {
            mLoading = null;
        }
        ButterKnife.unbind(this);
        MyApplication.getInstance().removeActivity(this);
    }

    protected abstract int getLayout();

    protected abstract void initEventAndData();

    protected abstract void  createPresenter();


    protected final void showLoading(@StringRes int textResId) {
        showLoading(getString(textResId));
    }
    protected final void showLoading() {
        cancelLoading();
        if (mLoading == null) {
            mLoading = new LoadingDialog(this);
            mLoading.setCancelable(false);
            mLoading.setCanceledOnTouchOutside(false);
        }
            mLoading.setTitle("请等待...");
            mLoading.show();
    }
    protected final void showLoading(String text) {
        cancelLoading();
        if (mLoading == null) {
            mLoading = new LoadingDialog(this);
            mLoading.setCancelable(false);
            mLoading.setCanceledOnTouchOutside(false);
        }
        mLoading.setTitle(text);
        mLoading.show();
    }
    protected final void cancelLoading() {
        if (mLoading != null && mLoading.isShowing()) {
            mLoading.dismiss();
        }
    }

    @Override
    public void finish() {
        super.finish();
        KeyboardUtils.hideSoftInput(this);
    }
}
